package com.ybfenpan.service;


import com.ybfenpan.model.Admin;
import com.ybfenpan.model.Member;
import com.ybfenpan.model.SysPay;
import com.ybfenpan.model.log.*;
import com.ybfenpan.service.core.BaseService;
import com.ybfenpan.service.log.LogEleService;
import com.ybfenpan.service.log.LogFutouService;
import com.ybfenpan.service.log.LogMoneyService;
import com.ybfenpan.service.log.LogShuiService;
import com.ybfenpan.utils.ContextUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


@Service
public class SysPayService extends BaseService {

    @Resource
    private MemberService memberService;
    @Resource
    private SysPayService sysPayService;
    @Resource
    private LogMoneyService logMoneyService;

    @Resource
    private LogEleService logEleService;

    @Resource
    private LogShuiService logShuiService;

    @Resource
    private LogFutouService logFutouService;

    /**
     * 保存
     *
     * @param member
     */


    public void save(SysPay sysPay, Member member) throws InterruptedException {
        // 新增记录
        Admin admin = ContextUtil.getAdmin();
        sysPay.setMemberId(member.getId());
        sysPay.setAdminId(admin.getId());
        this.saveOrUpdate(sysPay);

        Double money = sysPay.getMoney();
        Double ele = sysPay.getEle();
        Double chuangye = sysPay.getChuangye();
        Double shui = sysPay.getShui();
        Double store = sysPay.getStore();
        Double shopping = sysPay.getShopping();
        Double futou = sysPay.getFutou();
        Double dianzi = sysPay.getDianzi();

        System.out.println("sysPay.getType():" + sysPay.getType());


        if (money != 0) {

            Map<String, Object> mapMember = new HashMap<String, Object>();
            mapMember.put("id", member.getId());
            mapMember.put("diyUpdate", "money=money+" + money);
            // 更新余额
            memberService.update(mapMember);

            member = memberService.getByKey(member.getId());
            Double yu = member.getMoney();

            LogMoney logMoney = new LogMoney();
            logMoney.setMemberId(member.getId());
            logMoney.setMemberLoginName(member.getLoginName());

            if (sysPay.getContent().length() > 0 && sysPay.getContent() != null) {
                logMoney.setTitle("系统充值，充值数：" + futou + "，备注：" + sysPay.getContent());
                logMoney.setTitleEn("系统充值，充值数：" + futou + "，备注：" + sysPay.getContent());
            } else {
                logMoney.setTitle("系统充值，充值数：" + futou);
                logMoney.setTitleEn("系统充值，充值数：" + futou);
            }
            logMoney.setState(88);

            logMoney.setNum(money);
            logMoney.setYu(yu);

            logMoneyService.saveOrUpdate(logMoney);
        }

        if (futou != 0) {
            Map<String, Object> mapMember = new HashMap<String, Object>();
            mapMember.put("id", member.getId());
            mapMember.put("diyUpdate", "futou=futou+" + futou);
            // 更新余额
            memberService.update(mapMember);

            member = memberService.getByKey(member.getId());
            Double yu = member.getFutou();

            LogFutou logEle = new LogFutou();
            logEle.setMemberId(member.getId());
            logEle.setMemberLoginName(member.getLoginName());

            if (sysPay.getContent().length() > 0 && sysPay.getContent() != null) {
                logEle.setTitle("系统充值，充值数：" + futou + "，备注：" + sysPay.getContent());
                logEle.setTitleEn("系统充值，充值数：" + futou + "，备注：" + sysPay.getContent());
            } else {
                logEle.setTitle("系统充值，充值数：" + futou);
                logEle.setTitleEn("系统充值，充值数：" + futou);
            }
            logEle.setState(88);

            logEle.setNum(futou);
            logEle.setYu(yu);

            logFutouService.saveOrUpdate(logEle);

        }

        if (ele != 0) {
            Map<String, Object> mapMember = new HashMap<String, Object>();
            mapMember.put("id", member.getId());
            mapMember.put("diyUpdate", "ele=ele+" + ele);
            // 更新余额
            memberService.update(mapMember);

            member = memberService.getByKey(member.getId());
            Double yu = member.getEle();

            LogEle logEle = new LogEle();
            logEle.setMemberId(member.getId());
            logEle.setMemberLoginName(member.getLoginName());

            if (sysPay.getContent().length() > 0 && sysPay.getContent() != null) {
                logEle.setTitle("系统充值，充值数：" + futou + "，备注：" + sysPay.getContent());
                logEle.setTitleEn("系统充值，充值数：" + futou + "，备注：" + sysPay.getContent());
            } else {
                logEle.setTitle("系统充值，充值数：" + futou);
                logEle.setTitleEn("系统充值，充值数：" + futou);
            }
            logEle.setState(88);

            logEle.setNum(ele);
            logEle.setYu(yu);

            logEleService.saveOrUpdate(logEle);

        }

        if (shui != 0) {
            Map<String, Object> mapMember = new HashMap<String, Object>();
            mapMember.put("id", member.getId());
            mapMember.put("diyUpdate", "shui=shui+" + shui);
            // 更新余额
            memberService.update(mapMember);

            member = memberService.getByKey(member.getId());
            Double yu = member.getShui();

            LogShui logShui = new LogShui();
            logShui.setMemberId(member.getId());
            logShui.setMemberLoginName(member.getLoginName());

            if (sysPay.getContent().length() > 0 && sysPay.getContent() != null) {
                logShui.setTitle("系统充值，充值数：" + futou + "，备注：" + sysPay.getContent());
                logShui.setTitleEn("系统充值，充值数：" + futou + "，备注：" + sysPay.getContent());
            } else {
                logShui.setTitle("系统充值，充值数：" + futou);
                logShui.setTitleEn("系统充值，充值数：" + futou);
            }
            logShui.setState(88);

            logShui.setNum(shui);
            logShui.setYu(yu);

            logShuiService.saveOrUpdate(logShui);

        }
    }
}
